﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;

namespace fct_api_test
{
    public class TransofromDataSetToXML
    {
        string sqlConnection;
        SqlConnection sqlcnnx;
        public TransofromDataSetToXML()
        {
            sqlConnection = ConfigurationManager.ConnectionStrings["codeshare"].ConnectionString;
            sqlcnnx = new SqlConnection(sqlConnection);
            sqlcnnx.Open();
            TryTransformation();
           
        }






        public string TryTransformation()
        {

            SqlDataAdapter adapter = new SqlDataAdapter("Select * From AGENDA", sqlcnnx);
            // DataSet dsPubs = new DataSet("");
            DataSet _dataset = new DataSet("test");
            //DataTable _datatable = new DataTable("AGENDA");
            //adapter.FillSchema(_dataset, SchemaType.Source, "AGENDA");
            //adapter.Fill(_dataset, "AGENDA");
            adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            adapter.Fill(_dataset, "AGENDA");
            DataTable tableAgenda;
            tableAgenda = _dataset.Tables["AGENDA"];
            _dataset.WriteXml(HttpContext.Current.Server.MapPath("XMLFile1.xml"));
            _dataset.Dispose();// On libère les ressources du DataSet
            _dataset.Reset();// On nettoie le D
            _dataset.Dispose();// On libère les ressources du DataTable
            _dataset.Reset(); // On nettoie le DataTable
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath("XMLFile1.xml"));
            XmlNode root = doc.DocumentElement;
            string xmlData = _dataset.GetXml();
            return xmlData;
        }
    }
}